package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;

import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ComplaintDTO;
import com.api.shopapi.entity.dto.ElderlyDTO;
import com.api.shopapi.service.ComplaintService;
import com.api.shopapi.service.ElderlyService;
import com.api.shopapi.service.StaffService;
import com.api.shopapi.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 投诉建议
 */
@RestController
@RequestMapping("Complaint")
public class ComplaintController extends BaseController{

    @Autowired
    @Qualifier("ComplaintServiceImplOne")
    private ComplaintService complaintService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("ElderlyServiceImplOne")
    private ElderlyService elderlyService;


    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");
            String remarks = jsonObject.getString("remarks");

            int type = jsonObject.getInteger("type");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Complaint/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            User user =userService.getByUserCode(userCode);
            String governmentCode=user.getGovernmentCode();
            String complaintCode=UUID.randomUUID().toString();

            Complaint complaint=new Complaint();
            complaint.setComplaintCode(complaintCode);
            complaint.setUserCode(userCode);
            complaint.setType(type);
            complaint.setRemarks(remarks);
            complaint.setCreateTime(new Date());
            complaint.setGovernmentCode(governmentCode);
            complaint.setStatus(2);

            complaintService.insert(complaint);

            Object data = new Object[]{};
            return success(data);
        } catch (Exception e) {
            insertError("/Complaint/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //评价列表
    @RequestMapping(value = "PageList",method = RequestMethod.POST)
    public  MyResult PageList(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize=jsonObject.getInteger("pageSize");
            int status =jsonObject.getInteger("status");
            int type=jsonObject.getInteger("type");

            String startTimeStr = jsonObject.getString("startTime");
            String endTimeStr = jsonObject.getString("endTime");

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime = sdf.parse(startTimeStr);
            Date endTime = sdf.parse(endTimeStr);

            Calendar calendar = new GregorianCalendar();
            calendar.setTime(endTime);
            calendar.add(calendar.DATE, 1); //把日期往后增加一天,整数  往后推,负数往前移动
            endTime = calendar.getTime(); //这个时间就是日期往后推一天的结果

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Complaint/PageList");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Staff staff =staffService.getByStaffCode(staffCode);
            String governmentCode=staff.getGovernmentCode();
            PageInfo<ComplaintDTO> complaintPageInfo=complaintService.getPageList(pageIndex,pageSize,governmentCode,status,type,startTime,endTime);
            List<ComplaintDTO> complaintPageInfoList=complaintPageInfo.getList();
            for (ComplaintDTO complaintDTO:complaintPageInfoList){
                Complaint complaint =complaintService.getByComplaintCode(complaintDTO.getComplaintCode());
                String userCode=complaint.getUserCode();
                User user =userService.getByUserCode(userCode);
                complaintDTO.setUser(user);
            }
            return success(complaintPageInfo);
        } catch (ParseException e) {
//            e.printStackTrace();
            insertError("/Complaint/PageList",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //处理投诉意见
    @RequestMapping(value = "Dispose",method = RequestMethod.POST)
    public MyResult Dispose(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String complaintCode=jsonObject.getString("complaintCode");
            String disposeRemark =jsonObject.getString("disposeRemark");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Complaint/Dispose");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            
            Complaint complaint = complaintService.getByComplaintCode(complaintCode);
            complaint.setStatus(1);
            complaint.setDisposeRemark(disposeRemark);
            complaintService.update(complaint);
            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Complaint/Dispose",jsonObject.toJSONString(),getExceptionMsg(e),"api");
            return anomalous(getExceptionMsg(e));
        }
    }


}
